package model;

import org.jblas.DoubleMatrix;

import runnable.Main;
import util.Params;
import adt.Sentence;

/**
 * Lookup table layer of the network.
 * 
 * 神经网络的查询层。
 * 
 * @author Tianyu Xu
 * 
 */
public class LookupTableLayer {
	/**
	 * Get a semi-finished feature matrix. It has proper size, but only filled
	 * with trainable features and POS tag features.
	 * 
	 * 生成一个半成品特征矩阵。该矩阵有正确的大小，并填充了可训练特征和POS标签特征，但没有添加谓词相对距离和involved字符相对距离。
	 * 
	 * @param sentence
	 *            要生成特征矩阵的句子
	 * @return 输入句子的半成品的特征矩阵
	 */
	public DoubleMatrix getSemiFinishedFeatureMatrix(Sentence sentence) {
		String str = sentence.toString();
		DoubleMatrix outputMatrix = new DoubleMatrix(Params.FEATURE_VECTOR_SIZE, str.length());

		DoubleMatrix wordVector = null;
		for (int i = 0; i < str.length(); i++) {
			// add trainable word feature vectors
			// 添加字特征
			char c = str.charAt(i);
			wordVector = Main.wordDict.get(c);

			// add POS tagging features
			// 添加POS标签特征
			int posTagNum = sentence.wordAt(i).getPosTagNum();
			wordVector = DoubleMatrix.concatVertically(wordVector, Main.posDict.get(posTagNum));

			// set the last two bits
			// 将倒数第二位标记为“未知其与谓词的距离”，将最后一位标记为“未考虑”
			wordVector = DoubleMatrix.concatVertically(wordVector, new DoubleMatrix(2, 1, new double[] { 0, 0 }));

			outputMatrix.putColumn(i, wordVector);
		}
		return outputMatrix;
	}
}
